package me.numark.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


import butterknife.Bind;
import butterknife.ButterKnife;
import me.numark.R;
import me.numark.activities.ChatActivity;
import me.numark.activities.ContactsActivity;
import me.numark.model.User;
import me.numark.utils.Constants;
import me.numark.utils.Global;
import me.numark.utils.Utils;
import me.numark.view.PagerSlidingTabStrip;

public class NoticeFragment extends Fragment implements
        //RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener, View.OnClickListener {
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.pager)
    ViewPager pager;

  /*  @Bind(R.id.radio_left)
    RadioButton leftRadio;

    @Bind(R.id.radio_center)
    RadioButton centerRadio;

    @Bind(R.id.radio_right)
    RadioButton rightRadio;*/
    @Bind(R.id.bottom_line)
    View bottomLine;
    @Bind(R.id.write_msg)
    LinearLayout writeMsgLayout;
/*
    @Bind(R.id.radio_group)
    RadioGroup radioGroup;*/
    @Bind(R.id.show_tabss)
    PagerSlidingTabStrip tabs;
    /*@Bind(R.id.right_image)
    ImageView rightImage;*/
    @Bind(R.id.toolbar_title)
    TextView textTitle;
    public static final int REQUEST_CONTACTS = 10001;

    NoticeFragmentPageAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tabs_2, container, false);
        ButterKnife.bind(this, v);
        toolbar.setVisibility(View.GONE);
        textTitle.setVisibility(View.GONE);
        textTitle.setText(getString(R.string.msg_notice));
        bottomLine.setVisibility(View.GONE);
        writeMsgLayout.setVisibility(View.GONE);
        mAdapter = new NoticeFragmentPageAdapter();
        pager.setAdapter(mAdapter);
        pager.addOnPageChangeListener(this);
        /*centerRadio.setVisibility(View.VISIBLE);
        leftRadio.setText(mAdapter.getPageTitle(0));
        centerRadio.setText(mAdapter.getPageTitle(1));
        rightRadio.setText(mAdapter.getPageTitle(2));*/
        /*rightImage.setVisibility(View.VISIBLE);
        rightImage.setImageResource(R.drawable.ic_msg);
        rightImage.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(this);*/
        writeMsgLayout.setOnClickListener(this);
        //radioGroup.setVisibility(View.GONE);


        tabs.setVisibility(View.VISIBLE);
        tabs.setShouldExpand(true);
        tabs.setIndicatorColorResource(R.color.btn_green);
        tabs.setUnderlineColorResource(R.color.gray0);
        tabs.setCheckedTextColorResource(R.color.btn_green);
        tabs.setViewPager(pager);
        return v;
    }

   /* @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radio_right:
                pager.setCurrentItem(2);
                break;
            case R.id.radio_center:
                pager.setCurrentItem(1);
                break;
            default:
                pager.setCurrentItem(0);
                break;
        }
    }*/

    @Override
    public void onPageScrolled(int i, float v, int i1) {
        tabs.onPageScrolled(i, v, i1);
    }

    @Override
    public void onPageSelected(int i) {
       /* switch (i) {
            case 2:
                radioGroup.check(R.id.radio_right);
                break;
            case 1:
                radioGroup.check(R.id.radio_center);
                break;
            default:
                radioGroup.check(R.id.radio_left);
                break;

        }*/
        if (i == 1){
            bottomLine.setVisibility(View.VISIBLE);
            writeMsgLayout.setVisibility(View.VISIBLE);
        }else{
            bottomLine.setVisibility(View.GONE);
            writeMsgLayout.setVisibility(View.GONE);
        }
        tabs.onPageSelected(i);
    }

    @Override
    public void onPageScrollStateChanged(int i) {
        tabs.onPageScrollStateChanged(i);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.write_msg:
                startActivityForResult(new Intent(getActivity(), ContactsActivity.class), REQUEST_CONTACTS);
                break;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK)
            return;
        switch (requestCode) {
            case REQUEST_CONTACTS: {
                String userStr = data.getStringExtra(Constants.USER_EXTRA);
                User user = Global.getGson().fromJson(userStr, User.class);
                ChatActivity.startChat(getActivity(), user);
            }
            break;
        }
    }

    NoticeCommentFragment commentFragment = null;
    NoticeLikeFragment likeFragment = null;
    NoticeMsgFragment msgFragment = null;

    private class NoticeFragmentPageAdapter extends FragmentPagerAdapter {

        public NoticeFragmentPageAdapter() {
            super(getChildFragmentManager());
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
               /* case 0:
                    if (commentFragment == null)
                        commentFragment = new NoticeCommentFragment();
                    return commentFragment;*/
                case 0:
                    if (likeFragment == null)
                        likeFragment = new NoticeLikeFragment();
                    return likeFragment;
                case 1:
                    if (msgFragment == null)
                        msgFragment = new NoticeMsgFragment();
                    return msgFragment;
                default:
                    return new Fragment();
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
               /* case 0:
                    return getString(R.string.comment);*/
                case 0:
                    return getString(R.string.notice);
                case 1:
                    return getString(R.string.private_message);
                default:
                    return "";
            }
        }
    }

}
